package com.share.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.constant.MessageConstant;
import com.share.common.core.exception.ServiceException;
import com.share.common.security.utils.SecurityUtils;
import com.share.domain.MySql.PowerBank;
import com.share.service.PowerBankService;
import com.share.mapper.PowerBankMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 肆
 * @description 针对表【power_bank(充电宝表)】的数据库操作Service实现
 * @createDate 2025-03-25 14:25:24
 */
@Service
public class PowerBankServiceImpl extends ServiceImpl<PowerBankMapper, PowerBank> implements PowerBankService {

    @Autowired
    private PowerBankMapper powerBankMapper;

    /**
     * 分页查询充电宝接口
     *
     * @param powerBank
     * @return
     */
    @Override
    public List<PowerBank> selectPowerBankList(PowerBank powerBank) {
        return powerBankMapper.selectPowerBankList(powerBank);
    }


    /**
     * 新增充电宝接口
     *
     * @param powerBank
     * @return
     */
    @Override
    public int savePowerBank(PowerBank powerBank) {
        //1 判断充电宝编号是否存在，如果存在不进行添加
        long count = this.count(new LambdaQueryWrapper<PowerBank>().eq(PowerBank::getPowerBankNo, powerBank.getPowerBankNo()));
        if (count > 0) {
            throw new ServiceException(MessageConstant.NUMBER_ALREADY_IN_USE);
        }
        powerBankMapper.insert(powerBank);
        return 1;
    }


    /**
     * 修改充电宝接口
     *
     * @param powerBank
     * @return
     */
    @Override
    public int updatePowerBank(PowerBank powerBank) {
        //获取旧的数据
        PowerBank oldPowerBank = powerBankMapper.selectById(powerBank.getId());
        //判断状态是0才进行修改
        if (oldPowerBank != null && !"0".equals(oldPowerBank.getStatus())) {
            throw new ServiceException(MessageConstant.USED_NO_CHANGES);
        }
        //判断柜机编号是否改变
        if (!oldPowerBank.getPowerBankNo().equals(powerBank.getPowerBankNo())) {
            //编号改变，判断编号是否存在
            long count = this.count(new LambdaQueryWrapper<PowerBank>().eq(PowerBank::getPowerBankNo, powerBank.getPowerBankNo()));
            if (count > 0) {
                throw new ServiceException(MessageConstant.NUMBER_ALREADY_IN_USE);
            }
        }
        powerBankMapper.updateById(powerBank);
        return 1;
    }


    /**
     * 根据编号查询充电宝接口
     * @param powerBankNo
     * @return
     */
    @Override
    public PowerBank getByPowerBankNo(String powerBankNo) {
        return powerBankMapper.selectOne(new LambdaQueryWrapper<PowerBank>().eq(PowerBank::getPowerBankNo, powerBankNo));
    }


}




